package com.eascs.admin.o2o.specialInterview.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSONObject;
import com.eascs.admin.common.service.BaseSevice;
import com.eascs.admin.o2o.internal.util.BaseUserUtil;
import com.eascs.admin.o2o.internal.util.StatusContexts;
import com.eascs.admin.o2o.loan.service.O2oLoanapplyService;
import com.eascs.admin.o2o.specialInterview.dao.O2OSpecialInterviewDao;
import com.eascs.admin.o2o.specialInterview.entity.O2OSpecialInterview;
import com.eascs.app.log.Logger;
import com.eascs.app.log.LoggerFactory;
import com.eascs.workflow.common.service.wf.WorkFlowService;

@Service
public class O2OSpecialInterviewService extends BaseSevice {
	private final Logger logger = LoggerFactory.getLogger(O2OSpecialInterviewService.class);
	@Autowired
	private O2OSpecialInterviewDao o2OSpecialInterviewDao;

	@Autowired
	BaseUserUtil userUtils;
	@Reference
	private WorkFlowService workFlowService;
	@Autowired
	O2oLoanapplyService o2oLoanapplyService;
	
	@Value("${o2o.admin.backfeeUrl}")
	private String backfeeUrl;
	
	public O2OSpecialInterview queryEntity(String loanapplyNo){
		String depositSql="select * from o2o_special_interview t where islock = 'N' And t.loanapply_no=?  ";
		
		return o2OSpecialInterviewDao.findEntity(depositSql, O2OSpecialInterview.class,loanapplyNo);
	}
	
	public void save(O2OSpecialInterview o2OSpecialInterview) {
		logger.info("o2OSpecialInterview:" + JSONObject.toJSONString(o2OSpecialInterview));
		o2OSpecialInterviewDao.insert(o2OSpecialInterview);
	}
	
	
	@Transactional(transactionManager = "o2oTransactionManager")
	public void updateWorkFlowStatus(String status, String loanapplyNo) {
		if (status.equals(StatusContexts.WFTYPE_STATUS_NEW)) { // 新建然后run
			String sql = "update o2o_special_interview o set o.updator=?,o.update_time=? where o.loanapply_no=? and bill_status = ? ";
			o2OSpecialInterviewDao.execute(sql, userUtils.getUpdateOr(), userUtils.getUpdateTime(), loanapplyNo, status );
		} else {
			String sql = "update o2o_special_interview o set o.bill_status=?,o.updator=?,o.update_time=? where o.loanapply_no=? and bill_status = ? ";
			o2OSpecialInterviewDao.execute(sql, status, userUtils.getUpdateOr(), userUtils.getUpdateTime(), loanapplyNo,"NEW");
		}
	}
	
	public int update(O2OSpecialInterview o2OSpecialInterview) {
		logger.info("o2OSpecialInterview:" + JSONObject.toJSONString(o2OSpecialInterview));
		return o2OSpecialInterviewDao.updateById(o2OSpecialInterview);
	}
	
	/**
	 * 是否已经存在run或者是clo状态的流程
	 * @param loanapplyNo
	 * @return
	 */
	public O2OSpecialInterview fingHasFlow( String loanapplyNo) {
		String sql = "SELECT * from o2o_special_interview   WHERE loanapply_no = ?  and islock = 'N' and bill_status != 'NEW' ";
		return o2OSpecialInterviewDao.findEntity(sql, O2OSpecialInterview.class, loanapplyNo);
	}
	
	
	/**
	 * 是否已经存在run或者是clo状态的流程
	 * @param loanapplyNo
	 * @return
	 */
	public O2OSpecialInterview fingCLOFlow( String loanapplyNo) {
		String sql = "SELECT * from o2o_special_interview   WHERE loanapply_no = ?  and islock = 'N' and bill_status = 'CLO' ";
		return o2OSpecialInterviewDao.findEntity(sql, O2OSpecialInterview.class, loanapplyNo);
	}
}
